<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;
use think\facade\Event;

class Mechanism extends Base
{
    protected $name = 'mechanism';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }


    public function getStatusAttr($value)
    {
        switch ($value) {
            case 1:
                return '待审核';
            case 2:
                return '已通过';
            case 3:
                return '已驳回';
            default:
                return '未知';
        }
    }


    public function getConfirmTimeAttr($value)
    {
        return date('Y-m-d H:i:s', intval($value));
    }


    public static function _save($param, $logs)
    {

        parent::startTrans();
        try {
            if (!isset($param['status'])) return ['code' => -1, 'msg' => '请选择审核类型'];

            $info = parent::find($param['id']);
            if (empty($info)) return ['code' => -1, 'msg' => '审核失败'];
            if ($info->getData('status') != 1) {
                return ['code' => -1, 'msg' => '此笔记录已审核，请勿重复审核！'];
            }
            if ($param['status'] == 2) {
                $msg = '通过激活申请，ID【' . $param['id'] . '】';
            } else {
                $msg = '驳回激活申请,ID【' . $param['id'] . '】';
            }

            $info->status = $param['status'];
            if ($param['status'] != 2) {
                $info->reject_msg = $param['reject_msg'];
            }
            $info->admin_id = $logs['admin']->id;
            $info->admin_name = $logs['admin']->admin_name;
            $info->confirm_time = time();
            $info->save();
            User::update(['type'=>2],['id'=>$info->uid]);
            Logs::adds($msg, $logs['admin']->id, $logs['admin']->admin_name);
            parent::commit();
            return ['code' => 1, 'msg' => '已审核'];

        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }


    public static function add($param, $user): array
    {
        try {
            $data = parent::where(['uid'=> $user->id])->find();
            if($data)
            {
                if($data->status==1)
                {
                    return ['code' => -1, 'msg' => '已提交，等待审核'];
                }elseif($data->status==2)
                {
                    return ['code' => -1, 'msg' => '已提交，无法重复提交'];
                }
            }
            $info = parent::create([
                'uid' => $user->id,
                'title' => $param['title'],
                'disseminate' => $param['disseminate'],
                'address' => $param['address'],
                'award_information' => $param['award_information'],
                'trainees_number' => $param['trainees_number'],
                'teacher_number' => $param['teacher_number'],
            ]);

            return ['code' => 1, 'msg' => '提交成功'];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
